#!/usr/bin/env python


# Try to find one library of a given list
# The first library found is stored in the given uselib_store
def check_lib_list(conf, uselib, uselib_store, lib_list):
	for lib in lib_list:
		if conf.check_cc(mandatory = 0, lib = lib, uselib = uselib, uselib_store = uselib_store):
			return True
	return False

def check_x11(conf, uselib = ''):
	return \
	  conf.check_cc(mandatory = 0, lib = 'X11', uselib = uselib, uselib_store = 'X11') and \
	  conf.check_cc(mandatory = 0, header_name = 'X11/Xlib.h', uselib = uselib, uselib_store = 'X11')

def check_wayland(conf, uselib = ''):
	return \
	  conf.check_cfg(mandatory = 0, package = 'wayland-client', uselib_store = 'WAYLAND_CLIENT', args = '--cflags --libs') and \
	  conf.check_cfg(mandatory = 0, package = 'wayland-cursor', uselib_store = 'WAYLAND_CURSOR', args = '--cflags --libs') and \
	  conf.check_cfg(mandatory = 0, package = 'wayland-egl',    uselib_store = 'WAYLAND_EGL',    args = '--cflags --libs')

def check_gles2(conf, uselib = 'EGL', lib_list = ['GLESv2']):
	retval = \
	  check_lib_list(conf = conf, uselib = uselib, uselib_store = 'GLES2', lib_list = lib_list) and \
	  conf.check_cc(mandatory = 0, header_name = 'GLES2/gl2.h', uselib = uselib, uselib_store = 'GLES2')
	conf.check_cc(mandatory = 0, header_name = ['GLES2/gl2.h', 'GLES2/gl2ext.h'], uselib = uselib, define_name = 'WITH_GL2EXT_H', uselib_store = 'GLES2')
	return retval

def check_vivante_egl(conf, egl_extra_defines, check_for_viv_header = False):
	if not conf.check_cc(mandatory = 0, lib = ['EGL'], uselib_store = 'EGL'):
		return False
	old_cflags = list(conf.env['CFLAGS'])
	extra_defines = ['LINUX']
	if egl_extra_defines:
		extra_defines += egl_extra_defines
	extra_cflags = map(lambda x: conf.env['DEFINES_ST'] % x, extra_defines)
	conf.env['CFLAGS'] += extra_cflags
	retval = True
	if check_for_viv_header:
		retval = retval and conf.check_cc(mandatory = 0, header_name = 'EGL/eglvivante.h', uselib_store = 'EGL')
	retval = retval and conf.check_cc(mandatory = 0, header_name = 'EGL/egl.h', uselib_store = 'EGL')
	conf.env['CFLAGS'] = old_cflags
	if retval:
		conf.env['DEFINES_EGL'] += extra_defines
	return retval


valid_egl_platforms = ['fb', 'x11', 'wayland', 'android']


def options(opt):
	opt.add_option('--disable-eglvivsink', action = 'store_true', default = False, help = 'disable eglvivsink plugin [default: enabled]')
	opt.add_option('--egl-platform', action='store', default='x11', help='EGL platform to build for (valid values: ' + ' '.join(valid_egl_platforms) + ')')


def check_dependencies(conf, platform):
	if platform == "x11":
		if not check_x11(conf) or not check_vivante_egl(conf, None):
			return False
		conf.env['PLATFORM_SOURCE'] = ['egl_platform_x11.c']
		conf.env['PLATFORM_USELIBS'] += ["X11"]
	elif platform == "fb":
		if not check_vivante_egl(conf, ['EGL_API_FB'], True):
			return False
		conf.env['PLATFORM_SOURCE'] = ['egl_platform_fb.c']
	elif platform == 'wayland':
		if not check_wayland(conf) or not check_vivante_egl(conf, ['EGL_API_FB', 'WL_EGL_PLATFORM']):
			return False
		conf.env['PLATFORM_SOURCE'] = ['egl_platform_wayland.c']
		conf.env['PLATFORM_USELIBS'] += ["WAYLAND_CLIENT", "WAYLAND_CURSOR", "WAYLAND_EGL"]
	elif platform == 'android':
		if not check_vivante_egl(conf, None):
			return False
		conf.env['PLATFORM_SOURCE'] = ['egl_platform_android.c']

	if not check_gles2(conf):
		return False

	return True


def configure(conf):
	from waflib.Build import Logs

	if conf.options.disable_eglvivsink:
		return

	if not conf.env['WITH_GSTVIDEO']:
		return

	platform = conf.options.egl_platform
	if conf.env['BUILD_FOR_ANDROID']:
		platform = 'android'

	if not (platform in valid_egl_platforms):
		conf.fatal('Invalid platform selected; run "./waf configure --help" for details about the --egl-platform option')

	if check_dependencies(conf, platform):
		Logs.pprint('GREEN', 'EGL sink with Vivante direct textures will be built')
		Logs.pprint('GREEN', 'Selected EGL platform: ' + platform)
		conf.env['EGLVIVSINK_ENABLED'] = 1
	else:
		Logs.pprint('RED', 'EGL sink with Vivante direct textures will not be built - dependencies not found')


def build(bld):
	if bld.env['EGLVIVSINK_ENABLED']:
		bld(
			features = ['c', bld.env['CLIBTYPE']],
			includes = ['../..'],
			use = 'gstimxcommon',
			uselib = ['EGL', 'GLES2'] + bld.env['PLATFORM_USELIBS'] + ['GSTREAMER_VIDEO'] + bld.env['COMMON_USELIB'],
			target = 'gstimxeglvivsink',
			source = ['eglvivsink.c', 'gl_headers.c', 'gles2_renderer.c', 'egl_misc.c', 'plugin.c'] + bld.env['PLATFORM_SOURCE'],
			install_path = bld.env['PLUGIN_INSTALL_PATH']
		)

